package com.siwei.mes.job;

import cn.hutool.json.JSONUtil;
import com.siwei.mes.common.util.DateHelper;
import com.siwei.mes.entity.experiment.ExperimentCheckWaybill;
import com.siwei.mes.entity.experiment.ExperimentGenConfig;
import com.siwei.mes.enums.MaterialsEnum;
import com.siwei.mes.mapper.experiment.ExperimentCheckWaybillMapper;
import com.siwei.mes.mapper.experiment.ExperimentGenConfigMapper;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 根据星期定时清空原材料相关
 *
 * @Description: 根据星期定时清空原材料相关
 * @Version: 1.0
 * @author: yangke
 * @date: 2024/07/20 00:41
 */
@Component
public class RawMaterialsWeekJob {
    private static final Logger log = LoggerFactory.getLogger("job");
    @Resource
    private ExperimentGenConfigMapper experimentGenConfigMapper;
    @Resource
    private ExperimentCheckWaybillMapper experimentCheckWaybillMapper;

    /**
     * 每日清空一次
     */
    @Scheduled(cron = "0 0 0 1/1 * ? ")
    public void init() {
        log.info("根据星期定时清空原材料相关开始");
        List<ExperimentGenConfig> genConfigList = experimentGenConfigMapper.getList(new ExperimentGenConfig().setIsOpen(1).setCheckRules(4));
        for (ExperimentGenConfig genConfig : genConfigList) {
            if (!Objects.equals(genConfig.getExperimentType(), MaterialsEnum.CONCRETE.getCode())) {
                /**这里的时间可能是多个 用逗号给开的*/
                String checkThreshold = genConfig.getCheckThreshold();

                List<String> checkDays = Arrays.asList(checkThreshold.split(","));

                if (StringUtils.isNotBlank(checkThreshold) && checkDays.contains(String.valueOf(DateHelper.getWeek()))) {
                    ExperimentCheckWaybill checkWaybill = new ExperimentCheckWaybill();
                    checkWaybill.setCreateTime(new Date());
                    checkWaybill.setCheckType(genConfig.getCheckType());
                    checkWaybill.setExperimentType(genConfig.getExperimentType());
                    log.info("根据星期定时清空原材料相关，入参：【{}】", JSONUtil.toJsonStr(checkWaybill));
                    experimentCheckWaybillMapper.deleteByCreate(checkWaybill);
                }
            }
        }
    }
}
